//
//  NSObject+zll.m
//  test
//
//  Created by Vincent on 2017/4/12.
//  Copyright © 2017年 Vincent. All rights reserved.
//

#import "NSObject+zll.h"
#import <objc/runtime.h>

@implementation NSObject (zll)

-(void)setAssociatedObject:(id)obj forKey:(NSString *)key{
    
    objc_setAssociatedObject(self,[key UTF8String],obj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(id)associatedObjectForKey:(NSString *)key{
    
    return objc_getAssociatedObject(self, [key UTF8String]);
}

-(id)valueForUndefinedKey:(NSString *)key{
    
    return nil;
}

-(NSDictionary *)getAllProperties{
    
    NSMutableDictionary *dic = [NSMutableDictionary new];
    
    unsigned int count = 0;
    
    Ivar *ivars = class_copyIvarList(self.class, &count);
    
    for(int i=0;i<count;i++){
        const char *name = ivar_getName(ivars[i]);
        NSString *key = [NSString stringWithUTF8String:name];
        
        id val = [self valueForKey:key];
        if(val){
            dic[key] = val;
        }
    }
    
    return dic;
}


@end
